﻿<?php
include_once("../inc/global.php");
include_once("../bbs/include/common.inc.php");
include_once("../inc/parameters.php");

set_time_limit(0);

$str_root = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$str_root.= '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">' . "\n";

/* 生成Blog的Sitemaps */
$sql		= "select id, datetime from fund_blog_article order by id desc";
$result	= $_MyDB->query($sql);
$file_num = 1;
while($row = $_MyDB->fetch_array($result))
{
	$url		= date("Y_m", $row['datetime'])."/".date("Y_m_d", $row['datetime'])."/".$row['id'].".html";
	$lastmod	= date("Y-m-d", time());
	$str.= "	<url>\n";
	$str.= "		<loc>http://www.8caike.com/blog/writing/" . $url . "</loc>\n";
	$str.= "		<lastmod>".$lastmod."</lastmod>\n";
	$str.= "		<changefreq>daily</changefreq>\n";
	
	if($i == 0){
		$str.= "		<priority>0.7</priority>\n";
	}else{
		$str.= "		<priority>0.6</priority>\n";
	}
	$str.= "	</url>\n";
	
	$i++;
	
	//一个sitemaps文件放5000个链接
	if($i == 5000){
		$filename = "../sitemap_blog_". $file_num .".xml";
		$string	= $str_root;
		
		if($file_num == 1){
			$string.= $str_index;
		}
		
		$string.= $str . "</urlset>";
		make_file($filename, $string);
		
		$file_num++;
		$str = "";
		$i = 0;
	}
}

if($str != ""){
		$filename = "../sitemap_blog_". $file_num .".xml";
		$string		= $str_root . $str . "</urlset>";
		make_file($filename, $string);
}


function make_file($filename, $str)
{
	/* 生成XML文件 */
	if(!$handle = fopen($filename, 'w')) {
		print "不能打开文件 " . $filename . "<BR>";
		exit;
	}
	
	if (!fwrite($handle, $str)) {
		print "不能写入到文件 " . $filename . "<BR>";
		exit;
	}

	fclose($handle);
	echo "已更新到 " . $filename . "<BR>";
}
	
?>